<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   v-if="permission.tenant_delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
        <el-tooltip class="item" effect="dark" content="给租户配置账号额度、过期时间等授权信息" placement="top">
          <el-button size="small"
                     plain
                     v-if="userInfo.role_name.includes('administrator')"
                     icon="el-icon-setting"
                     @click="handleSetting">授权配置
          </el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="给租户配置独立数据源以实现数据库隔离" placement="top">
          <el-button size="small"
                     plain
                     v-if="userInfo.role_name.includes('administrator')"
                     icon="el-icon-coin"
                     @click="handleDatasource">数据源配置
          </el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="将菜单产品包与租户配置绑定" placement="top">
          <el-button size="small"
                     plain
                     v-if="userInfo.role_name.includes('administrator')"
                     icon="el-icon-notebook-1"
                     @click="handlePackage">产品包配置
          </el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="将自定义的菜单集合定制为租户绑定的菜单产品包" placement="top">
          <el-button size="small"
                     plain
                     v-if="userInfo.role_name.includes('administrator')"
                     icon="el-icon-notebook-2"
                     @click="handlePackageSetting">产品包管理
          </el-button>
        </el-tooltip>
      </template>
      <template slot-scope="" slot="idsForm">
          <div>
            <el-tabs type="border-card">
              <!-- <el-tab-pane label="菜单权限"> -->
                <div v-if="menuGrantList.length > 0 && openType != 'view'">
                  <el-button icon="el-icon-plus" size="mini" autofocus @click="menuGrantAll">全选</el-button>
                  <el-button icon="el-icon-minus" size="mini" autofocus @click="menuGrantEmpty">清空</el-button>
                </div>
                <el-tree :data="menuGrantList"
                        show-checkbox
                        node-key="id"
                        ref="treeMenu"
                        :default-checked-keys="menuTreeObj"
                        :props="props">
                </el-tree>
              <!-- </el-tab-pane> -->
            </el-tabs>
          </div>
      </template>
      <template slot-scope="{row}"
                slot="accountNumber">
        <el-tag>{{ row.accountNumber > 0 ? row.accountNumber : '不限制' }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="expireTime">
        <el-tag>{{ row.expireTime ? row.expireTime : '不限制' }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="isCooperate">
        <el-tag>{{ row.isCooperateName }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="profitState">
        <el-tag>{{ row.profitStateName }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="cooperateState">
        <el-tag>{{ row.cooperateStateName }}</el-tag>
      </template>
    </avue-crud>
    <el-dialog title="租户授权配置"
               append-to-body
               :visible.sync="box"
               width="450px">
      <avue-form :option="settingOption" v-model="settingForm" @submit="handleSubmit"/>
    </el-dialog>
    <el-dialog title="租户数据源配置"
               append-to-body
               :visible.sync="datasourceBox"
               width="450px">
      <avue-form :option="datasourceOption" v-model="datasourceForm" @submit="handleDatasourceSubmit"/>
    </el-dialog>
    <el-dialog title="租户产品包配置"
               append-to-body
               :visible.sync="packageBox"
               width="450px">
      <avue-form ref="formPackage" :option="packageOption" v-model="packageForm" @submit="handlePackageSubmit"/>
    </el-dialog>
    <el-dialog title="租户产品包管理"
               append-to-body
               :visible.sync="packageSettingBox"
               width="1000px">
      <tenant-package></tenant-package>
    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, getDetail, remove, update, add, setting, datasource, packageInfo, packageSetting} from "@/api/system/tenant";
import {getDetail as packageDetail} from "@/api/system/tenantpackage";
import {getRole, grantTree} from "@/api/system/role";
import {mapGetters} from "vuex";
import {getMenuTree} from "@/api/system/menu";
import {validatenull, isMobile} from "@/util/validate";

export default {
  data() {
    const validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'));
      } else if (!isMobile(value)) {
        callback(new Error('手机号码不正确！'));
      } else {
        callback();
      }
    };
    return {
      form: {},
      menuGrantList: [], //存放树形菜单权限的data
      menuTreeObj: [], //用户已有菜单权限数据
      openType: '', //打开dialog弹框的类型
      props: { //菜单权限数据 props
        label: "title",
        value: "key",
        disabled: this.isDisabledFn
      },
      selectionList: [],
      query: {},
      loading: true,
      userType: '1',
      box: false,
      datasourceBox: false,
      packageBox: false,
      packageSettingBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        delBtn: false,
        dialogWidth: 900,
        dialogClickModal: false,
        column: [
          {
            label: "租户ID",
            prop: "tenantId",
            width: 100,
            search: true,
            addDisplay: false,
            editDisplay: false,
            span: 24,
            hide: true,
            rules: [{
              required: true,
              message: "请输入租户ID",
              trigger: "blur"
            }]
          },
          {
            label: "租户名称",
            prop: "tenantName",
            search: true,
            span: 12,
            rules: [{
              required: true,
              message: "请输入租户名称",
              trigger: "blur"
            }]
          },
          {
            label: "租户类型",
            prop: "tenantType",
            type: "select",
            width: 90,
            span: 12,
            props: {
              label: "label",
              value: "value"
            },
            dicData: [
              {
                label: "一般纳税人",
                value: 1
              },
              {
                label: "小规模经营",
                value: 2
              }
            ],
            rules: [{
              required: true,
              message: "请输入租户类型",
              trigger: "blur"
            }]
          },
          {
            label: "联系人",
            prop: "linkman",
            search: true,
            rules: [{
              required: true,
              message: "请输入联系人",
              trigger: "blur"
            }]
          },
          {
            label: "联系电话",
            prop: "contactNumber",
            width: 100,
            rules: [{
              required: true,
              trigger: "blur",
              validator: validatePhone
            }]
          },
          {
            label: '省份',
            prop: 'provinceName',
            slot: true,
            display: false,
          },
          {
            label: '地市',
            prop: 'cityName',
            slot: true,
            display: false
          },
          {
            label: '区县',
            prop: 'areaName',
            slot: true,
            display: false
          },
          {
            label: '省份',
            prop: 'province',
            type: 'select',
            props: {
              label: 'name',
              value: 'code'
            },
            cascaderItem: ['city', 'area'],
            dicUrl: '/api/blade-system/region/select',
            span: 8,
            showColumn: false,
            hide: true,
            rules: [{
              required: true,
              message: "请选择省份",
              trigger: "blur"
            }]
          },
          {
            label: '地市',
            prop: 'city',
            type: 'select',
            props: {
              label: 'name',
              value: 'code'
            },
            dicUrl: '/api/blade-system/region/select?code={{key}}',
            span: 8,
            showColumn: false,
            hide: true,
            rules: [{
              required: true,
              message: "请选择地市",
              trigger: "blur"
            }]
          },
          {
            label: '区县',
            prop: 'area',
            type: 'select',
            props: {
              label: 'name',
              value: 'code'
            },
            dicUrl: '/api/blade-system/region/select?code={{key}}',
            span: 8,
            showColumn: false,
            hide: true,
            rules: [{
              required: true,
              message: "请选择区县",
              trigger: "blur"
            }]
          },
          {
            label: "联系地址",
            prop: "address",
            span: 24,
            minRows: 2,
            type: "textarea",
            hide: true,
            rules: [{
              required: true,
              message: "请输入联系地址",
              trigger: "blur"
            }]
          },
          {
            label: "账号额度",
            prop: "accountNumber",
            width: 90,
            slot: true,
            addDisplay: false,
            editDisplay: false,
          },
          {
            label: "过期时间",
            prop: "expireTime",
            width: 160,
            slot: true,
            span: 24,
            addDisplay: false,
            editDisplay: false,
          },
          {
            label: "合作状态",
            prop: "isCooperate",
            span: 24,
            type: "switch",
            width: 100,
            dicUrl: "/api/blade-system/dict/dictionary?code=is_cooperate",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            value: '1',
            slot: true,
            addDisplay: false,
            hide: true,
          },
          {
            label: "分润状态",
            prop: "profitState",
            span: 12,
            type: "radio",
            width: 100,
            dicUrl: "/api/blade-system/dict/dictionary?code=profit_state",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            value: '2',
            slot: true,
            addDisplay: false,
            hide: true,
          },
          {
            label: "合同状态",
            prop: "cooperateState",
            span: 12,
            type: "radio",
            width: 110,
            dicUrl: "/api/blade-system/dict/dictionary?code=cooperate_state",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            value: '0',
            slot: true,
            addDisplay: false,
            hide: true,
          },
          {
            label: "绑定域名",
            prop: "domain",
            span: 24,
            prepend:'https://',
            append:'com',
            rules: [{
              required: true,
              message: "请输入域名地址",
              trigger: "blur"
            }]
          },
          {
            label: "系统背景",
            prop: "backgroundUrl",
            type: 'upload',
            listType: 'picture-img',
            dataType: 'string',
            action: '/api/blade-resource/oss/endpoint/put-file',
            propsHttp: {
              res: 'data',
              url: 'link',
            },
            hide: true,
            span: 24,
          },
          {
            prop:'ids',
            span: 24,
            labelWidth: 0,
            formslot: true,
            showColumn: false,
            slot: true,
            hide: true
          },
        ]
      },
      data: [],
      settingForm: {},
      settingOption: {
        column: [
          {
            label: "账号额度",
            prop: "accountNumber",
            type: "number",
            span: 24,
          },
          {
            label: "过期时间",
            prop: "expireTime",
            type: "date",
            format: "yyyy-MM-dd hh:mm:ss",
            valueFormat: "yyyy-MM-dd hh:mm:ss",
            span: 24,
          }
        ]
      },
      datasourceForm: {},
      datasourceOption: {
        column: [
          {
            label: "数据源",
            prop: "datasourceId",
            search: true,
            span: 24,
            type: "select",
            dicUrl: "/api/blade-develop/datasource/select",
            props: {
              label: "name",
              value: "id"
            },
            rules: [{
              required: true,
              message: "请选择数据源",
              trigger: "blur"
            }]
          },
        ]
      },
      packageForm: {},
      packageOption: {
        column: [
          {
            label: "产品包",
            prop: "packageId",
            search: true,
            span: 24,
            type: "select",
            dicUrl: "/api/blade-system/tenant-package/select",
            props: {
              label: "packageName",
              value: "id"
            },
            rules: [{
              required: true,
              message: "请选择产品包",
              trigger: "blur"
            }]
          },
          {
            label: "菜单预览",
            prop: "menuId",
            span: 24,
            type: "tree",
            dicData: [],
            hide: true,
            multiple: true,
            props: {
              label: "title"
            },
          },
        ]
      },
    };
  },
  watch: {
    'packageForm.packageId'() {
      if (!validatenull(this.packageForm.packageId)) {
        packageDetail(this.packageForm.packageId).then(res => {
          this.packageForm.menuId = res.data.data.menuId;
          this.initData();
        });
      }
    }
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.tenant_add, false),
        viewBtn: this.vaildData(this.permission.tenant_view, false),
        delBtn: this.vaildData(this.permission.tenant_delete, false),
        editBtn: this.vaildData(this.permission.tenant_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    tenantId() {
      return this.selectionList[0].tenantId;
    }
  },
  methods: {
    initData() {
      getMenuTree().then(res => {
        const column = this.findObject(this.packageOption.column, "menuId");
        column.dicData = res.data.data;
      });
    },
    rowSave(row, done, loading) {
      //合同等状态 初始化赋值
      row.isCooperate = this.form.isCooperate;
      row.profitState = this.form.profitState;
      row.cooperateState = this.form.cooperateState;
      const menuList = this.$refs.treeMenu.getCheckedKeys();
      row.ids = menuList;
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      const menuList = this.$refs.treeMenu.getCheckedKeys();
      row.ids = menuList;
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    /**
     * @func dialog打开回调
     * @desc 监听回调类型，调取接口，处理逻辑
     * @param {Function} done - 回调完成
     * @param {String} type - 打开类型
     * */
    beforeOpen(done, type) {
      this.menuTreeObj = [];
      this.openType = type;
      if (["view", "edit"].includes(type)) {
        getDetail(this.form.id).then(res => {
          const data = res.data.data;
          if (["view"].includes(type)) {
            if (!(data.accountNumber > 0)) {
              data.accountNumber = "不限制";
            }
            if (!data.expireTime) {
              data.expireTime = "不限制";
            }
          }
          data.isCooperate = data.isCooperate + '';
          data.profitState = data.profitState + '';
          data.cooperateState = data.cooperateState + '';
          this.form = data;
          grantTree(this.userType)
            .then(res => {
              this.menuGrantList = res.data.data.menu;
              getRole(this.form.userId).then(res => {
                this.menuTreeObj = res.data.data.menu;
              });
            });
        });
      } else if (["add"].includes(type)) {
        grantTree(this.userType)
          .then(res => {
            this.menuGrantList = res.data.data.menu;
            this.$nextTick(() => {
              this.$refs.treeMenu.setCheckedNodes(this.menuGrantList);
            });
          });
      }
      done();
    },
    /**
     * @func 菜单权限树形是否禁止
     * @desc dialog打开类型为 view 则禁止权限树
     * @param {String} openType - 打开类型
     * */
    isDisabledFn() {
      const _this = this;
      if (["view"].includes(_this.openType)) {
        return true
      }
    },
    /**
     * @func 菜单权限全选
     * @desc 选中状态处理 (全选)
     * @param {Array} menuGrantList - 菜单权限集合
     * */
    menuGrantAll() {
      this.$nextTick(() => {
        this.$refs.treeMenu.setCheckedNodes(this.menuGrantList);
      });
    },
    /**
     * @func 菜单权限清空
     * @desc 选中状态处理 (清空)
     * @param {Array} menuGrantList - 菜单权限集合
     * */
    menuGrantEmpty() {
      this.$nextTick(() => {
        this.$refs.treeMenu.setCheckedKeys([]);
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    handleSetting() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      if (this.selectionList.length === 1) {
        getDetail(this.selectionList[0].id).then(res => {
          const data = res.data.data;
          this.settingForm.accountNumber = data.accountNumber;
          this.settingForm.expireTime = data.expireTime;
          this.settingForm.isCooperate = data.isCooperate;
          this.settingForm.profitState = data.profitState;
          this.settingForm.cooperateState = data.cooperateState;
        });
      } else {
        this.settingForm.accountNumber = -1;
        this.settingForm.expireTime = '';
      }
      this.box = true;
    },
    handleDatasource() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      if (this.selectionList.length !== 1) {
        this.$message.warning("只能选择一条数据");
        return;
      }
      getDetail(this.selectionList[0].id).then(res => {
        const data = res.data.data;
        this.datasourceForm.datasourceId = data.datasourceId;
      });
      this.datasourceBox = true;
    },
    handlePackage() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      if (this.selectionList.length !== 1) {
        this.$message.warning("只能选择一条数据");
        return;
      }
      if (this.selectionList.length === 1) {
        packageInfo(this.selectionList[0].id).then(res => {
          const data = res.data.data;
          this.packageForm.packageId = data.id;
          this.packageForm.menuId = data.menuId;
        });
      } else {
        this.packageForm.menuId = '';
      }
      this.packageBox = true;
      //更新字典远程数据
      setTimeout(() => {
        const form = this.$refs.formPackage;
        form.updateDic('packageId');
      }, 10);
    },
    handlePackageSetting() {
      this.packageSettingBox = true;
    },
    handleSubmit(form, done, loading) {
      setting(this.ids, form).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "配置成功!"
        });
        done();
        this.box = false;
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    handleDatasourceSubmit(form, done, loading) {
      datasource(this.tenantId, form.datasourceId).then(() => {
        this.$message({
          type: "success",
          message: "配置成功!"
        });
        done();
        this.datasourceBox = false;
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    handlePackageSubmit(form, done, loading) {
      packageSetting(this.tenantId, form.packageId).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "配置成功!"
        });
        done();
        this.packageBox = false;
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
